define(['app','assets/js/zTree'],function(app){
	return {module: function($scope, $http, eventBusService, httpService, params) {
		$('.leftMenu').css("height", $(window).height() - 70);

		var loadJavaScriptInHead = function() {
			var head = $('head');
			$("<script></script>").attr({ src: 'assets/js/zTree.excheck.js', type: 'text/javascript', id: 'loadscript' }).appendTo(head);
		}
		loadJavaScriptInHead();

		//树对象
		var zTreeObj;
		var currentNode;
		var zNodes = [];
		var zTreeOnClick = function(event, treeId, treeNode) {
			currentNode = treeNode;

			if(!currentNode.isParent) {
				window.scrollTo(0, 0);

				let url = treeNode.path;
				url += url.indexOf("?") >= 0 ? "&" : "?";
				var menu = {
					"url": url + "father_id=" + treeNode.id
				};

				$scope.defaultModeul = app.include(menu.url);
				$scope.$apply();

				$('.breadcrumb').html("");
				changeTreeNode(treeNode);
			}else{
				zTreeObj.expandNode(currentNode);
				if(currentNode.open) {
					$('#'+currentNode.tId+" .arrow > i").removeClass("icon-arrow-up").addClass("icon-arrow-down");
				}else{
					$('#'+currentNode.tId+" .arrow > i").removeClass("icon-arrow-down").addClass("icon-arrow-up");
				}
			}
		};

		var addDiyDom = function(treeId, treeNode) {
			var zTree = $("#" + treeNode.tId);
			var nodeName = $("#" + treeNode.tId + "_a .node_name");
			//如果根节点，显示图标
			if(treeNode.pId == null || treeNode.pId == "0") {
				nodeName.html("<i class=\"iconfont " + treeNode.icon + "\"></i>" + treeNode.name);
			}
			//如果是父节点，显示向下箭头
			if(treeNode.isParent) {
				zTree.append("<span class=\"arrow\"><i class=\"iconfont icon-arrow-up\"></i></span>");
			}
		};

		var str = "";
		var changeTreeNode = function(obj) {
			if(obj.pId == null || obj.pId == "ROOT") {
				$('.breadcrumb').append("<li class=\"breadcrumb-item\"><a href=\"\">基础信息</a></li><li class=\"breadcrumb-item\"><a href=\"\">"+obj.name+"</a></li>"+str);
			}else {
				for (let i = 0; i < zNodes.length; i++) {
					if (obj.pId == zNodes[i].id) {
						str = "<li class=\"breadcrumb-item\"><a href=\"\">" + obj.name + "</a></li>" + str;
						changeTreeNode(zNodes[i]);
						break;
					}
				}
				str = "";
			}
		}

		// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			view:{
				addDiyDom: addDiyDom,
				showIcon: false,
				showLine: false
			},
			callback: {
				onClick: zTreeOnClick
			}
		};
		// zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）

		//菜单查询
		var getMenuList = function() {
			$scope.form.fk_client = app.urls.appId;
			httpService.post(app.urls.menu_selectRoleMenuTree, $scope.form, function (data) {
				if ("0000" === data.code) {
					zNodes = data.data;

					zTreeObj = $.fn.zTree.init($("#zTree"), setting, zNodes);
					$scope.$apply();

					var nodes = zTreeObj.getNodes();
					if (nodes.length > 0) {
						currentNode = nodes[0];
						zTreeObj.selectNode(nodes[0]);
						setting.callback.onClick(null, zTreeObj.setting.treeId, nodes[0]);
						if(currentNode.isParent) {
							let children = currentNode.children;
							zTreeObj.selectNode(children[0]);
							setting.callback.onClick(null, zTreeObj.setting.treeId, children[0]);
						}
					}
				} else {
					alert(data.msg);
				}
			});
		}
		getMenuList();

		//接收模块变更
		eventBusService.subscribe('modules.content.load', function(event,data) {
			window.scrollTo(0, 0);
			$scope.defaultModeul = app.include(data.url);
		});
	}
}});